//
//  YZYMainViewController.swift
//  Y微Z博Y
//
//  Created by mac on 16/10/22.
//  Copyright © 2016年 mac. All rights reserved.
//

import UIKit

class YZYMainViewController: UITabBarController {

    override func viewDidLoad() {
        super.viewDidLoad()
        
        // 实例化tabbar
        let tabBar = YZYTabBar()
        
        // 02 闭包的实例化
        tabBar.closure = {
            // 04 接收回调
            print("在外面监听按钮点击")
        }
        
        // 通过KVC给计算属性赋值
        setValue(tabBar, forKey: "tabBar")
        // 创建字控制器
        addChildViewController(vc: YZYHomeViewController(), title: "首页", imgName: "tabbar_home")
        addChildViewController(vc: YZYMessageViewController(), title: "消息", imgName: "tabbar_message_center")
        addChildViewController(vc: YZYDiscoverViewController(), title: "发现", imgName: "tabbar_discover")
        addChildViewController(vc: YZYProfileViewController(), title: "我", imgName: "tabbar_profile")
    }
    
    
    /// 添加子控制器
    ///
    /// - parameter vc:      子控制器
    /// - parameter title:   名字
    /// - parameter imgName: 图片名
    private func addChildViewController(vc: UIViewController, title: String, imgName: String) {
        // 设置文字
        vc.title = title
//        vc.navigationItem = title
//        vc.tabBarItem = title
        // 设置颜色
        vc.tabBarItem.setTitleTextAttributes([NSForegroundColorAttributeName: YZY_Color], for: UIControlState.selected)
        // 设置图片
        vc.tabBarItem.image = UIImage(named: imgName)
        // 选中图片(alwaysOriginal 原生渲染方式)
        vc.tabBarItem.selectedImage = UIImage(named: "\(imgName)_selected")?.withRenderingMode(UIImageRenderingMode.alwaysOriginal)
        // 添加
        let nav = YZYNavigationController(rootViewController: vc)
        addChildViewController(nav)
        
    }
 
}
